package com.authine.cloudpivot.ext.service.impl;

import com.authine.cloudpivot.ext.asyncTask.AsyncTask;
import com.authine.cloudpivot.ext.domain.*;
import com.authine.cloudpivot.ext.mapper.*;
import com.authine.cloudpivot.ext.service.SynUserService;
import com.taobao.api.ApiException;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author liujing
 * @version 1.0.0
 * @ClassName GetUserServiceImpl.java
 * @Description
 * @Date: 2021/3/15
 */
@Service
@Slf4j
public class SynUserServiceImpl implements SynUserService {

    @Autowired
    private HOrgDepartmentMapper hOrgDepartmentMapper;
    @Autowired
    private HOrgDeptUserMapper hOrgDeptUserMapper;
    @Autowired
    private AsyncTask asyncTask;

    @Override
    public void synUser(String id, String appkey, String appsecret,String type) throws ApiException, BadHanyuPinyinOutputFormatCombination {
        long currentTimeMillis = System.currentTimeMillis();
        List<String> ids = new ArrayList<>();
        for (String deptId:getDeptIds(id,ids)){
            if ("1".equals(type)){
                Example example = new Example(HOrgDeptUser.class);
                example.createCriteria().andEqualTo("deptid",deptId);
                List<HOrgDeptUser> hOrgDeptUsers = hOrgDeptUserMapper.selectByExample(example);
                if (CollectionUtils.isEmpty(hOrgDeptUsers)){
                    asyncTask.getUserByOneDept(deptId,appkey,appsecret);
                }
            }else {
                asyncTask.getUserByOneDept(deptId,appkey,appsecret);
            }
         }
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("同步"+id+"总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
        }



    /**
     * @author liujing
     * @description: 递归获取部门最底层部门id集合
     * @date 2021/6/17
     * @param id
     * @param list
     * @return java.util.List<java.lang.String>
     */
    public List<String> getDeptIds (String id,List<String> list){
        List<String> list1 = hOrgDepartmentMapper.getSonDeptIds(id);
        if (CollectionUtils.isEmpty(list1)){
            list.add(id);
        } else {
            for (String s:list1){
                getDeptIds(s,list);
            }
        }
        return list;
    }

    @Override
    public void getUserByOneDept(String deptId, String appkey, String appsecret) throws ApiException, BadHanyuPinyinOutputFormatCombination {
        asyncTask.getUserByOneDept(deptId,appkey,appsecret);
    }



    @Override
    public void oneClickSync(String id, String appkey, String appsecret) throws ApiException, BadHanyuPinyinOutputFormatCombination {
        long currentTimeMillis = System.currentTimeMillis();
        List<String> ids = new ArrayList<>();
        for (String deptId:getDeptIds(id,ids)){
            asyncTask.getUserByOneDept(deptId,appkey,appsecret);
        }
        long currentTimeMillis1 = System.currentTimeMillis();
        log.info("同步"+id+"总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
    }
}
